package com.apex.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.util.FileUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class TableSchema {
    public static Map<String, JSONObject> parseTableSchema(String configFile){
        String tableName = "";
        Map<String,JSONObject> map = new HashMap<>();
        try {

//            String job = JSONObject.parseObject(JSONObject.parse(configFile).toString(),Feature.OrderedField).getString("job");

            String job = JSONObject.parseObject(FileUtils.readFileUtf8(new File(configFile)), Feature.OrderedField).getString("job");

            String transform = JSONObject.parseObject(job,Feature.OrderedField).getString("transform");

            String heapSchema = JSONObject.parseObject(transform,Feature.OrderedField).getString("schema");

            if (!StringUtils.isBlank(heapSchema)){
                for (Map.Entry<String, Object> entry :
                        JSONObject.parseObject(heapSchema).entrySet()) {
                    tableName = entry.getKey();
                }
            }
            String tableSchema = JSONObject.parseObject(heapSchema,Feature.OrderedField).getString(tableName);
            JSONObject jsonObject = JSONObject.parseObject(tableSchema, Feature.OrderedField);
            map.put(tableName,jsonObject);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
